package cn.mxz.generator.base;

import java.io.File;
import java.util.List;

import cn.mxz.generator.Generator;
import cn.mxz.generator.config.GeneratorConfig;
import cn.mxz.generator.config.IClass;
import cn.mxz.generator.config.IMethod;
import cn.mxz.generator.config.IReturn;

public abstract class GeneratorImpl implements Generator {

	protected String nodeName;
	
	protected GeneratorConfig config;
	
	public GeneratorImpl(GeneratorConfig config, String nodeName) {
		this.config = config;
		this.nodeName = nodeName;
	}
	
	/**
	 * 获得包路径
	 */
	protected String getPackageName() {
		String text = config.getNodeText(nodeName + ".package");
		return text;
	}
	
	/**
	 * 生成的文件夹路径
	 * @return
	 */
	protected String getSrcPath() {
		String s = config.getNodeText(nodeName + ".package").replaceAll("\\.", "\\" + File.separator);
		return config.getNodeText(nodeName) + File.separator + s;
	}

	protected String buildImports(IClass c) {
		List<IMethod> ms = c.getMethods();
		StringBuilder sb = new StringBuilder();
		for (IMethod m : ms) {
			IReturn rt = m.getReturn();
			if(!rt.isVoid() && !rt.isBaseType()) {
				sb.append("	import " + rt.getType() + ";\r");
			}
		}
		
		return sb.toString();
	}

	protected String buildImports(List<IClass> all) {
		StringBuilder sb = new StringBuilder();
		
		for (IClass c : all) {
			sb.append(buildImports(c));
		}
		return sb.toString();
	}
}
